<?php

namespace App\Models;

use App\User;
use App\Util\Tool;
use App\Util\Url;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;

class DoctorWorksBak extends Model
{
    protected $fillable = [
        'work_detail'
    ];
    protected $table = 'c_doctor_works_bak';

    public function clinic()
    {
        return $this->hasOne('App\Models\ClinicCategory', 'id', 'clinic_id');
    }

    public function course()
    {
        return $this->hasMany('App\Models\Course', 'work_id');
    }

    public function doctor()
    {
        return $this->hasOne('App\Models\DoctorBak','work_id');
    }

    public function province()
    {
        return $this->hasOne('App\Models\Area',"code","province_id");
    }

    public function city()
    {
        return $this->hasOne('App\Models\Area', "code", "city_id");
    }

    public function district()
    {
        return $this->hasOne('App\Models\Area', "code", "district_id");
    }

    public function setWorkDetailAttribute($gallery)
    {
        if (is_array($gallery)) {
            $this->attributes['work_detail'] = json_encode($gallery);
        }
    }

    public function getWorkDetailAttribute($gallery)
    {
        return json_decode($gallery, true);
    }

    /**
     * @return array
     * 获取工作室下拉列表
     */
    public function getOptions()
    {
        $options = $this->select('id', 'work_name as text')->get();
        $selectOption = [];
        foreach ($options as $option) {
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }

    /**获取分类名称
     * @param $id
     * @return mixed
     */
    public function getName($id)
    {
        $res = $this->select('work_name')->find($id);
        if (!empty($res)) {
            return $res->work_name;
        } else {
            return '';
        }
    }

    /***
     * 获取关注医护室文章
     */
    public function getWorksArticle($param)
    {
        $where = [];
        $work_id = (new Follow())->where([['user_id', '=', $param['user_id']], ['type', '=', 1]])->pluck("work_id");
        if (!empty($work_id)) {
            $where[] = [function ($query) use ($work_id) {
                return $query->whereIn('wra.work_id', $work_id);
            }];
            $res = (new Article())->join("c_work_relate_articles as wra", "wra.article_id", "=", "c_articles.id")
                ->where($where)->inRandomOrder()->limit(3)->get();
            if (!empty($res)) {
                $host = new Url();
                $doc = new Doctor();
                foreach ($res as $k => &$v) {
                    $v->art_img = $host::getHttpHost() . 'upload/' . $v->art_img;
                    $v->author = $doc->where([["id", '=', $v->doctor_id]])->value("real_name");
                    $v->content = strip_tags(str_replace("&nbsp;", '', $v->content));
                }
                return $res->toArray();
            }
            return [];
        }
        return [];
    }

    /***
     * @param $data
     * @return array|string
     * 保存草稿箱
     */

    public function createDoctorWorkBak($data)
    {
        try {
            DB::beginTransaction(); // 开始事务
//                更新
            $tool = new Tool();
            $redata  = self::where([['user_id','=',$data['user_id']]])->first();
            if ($redata) {
                $redata->user_id = isset($data['user_id']) ? $data['user_id'] : $redata->user_id;
                $redata->logo = isset($data['logo']) ? $data['logo'] : $redata->logo;
                $redata->work_name = isset($data['work_name']) ? $data['work_name'] : $redata->work_name;
                $redata->province_id = isset($data['province']) ? $data['province'] : $redata->province;
                $redata->city_id = isset($data['city']) ? $data['city'] : $redata->city;
                $redata->district_id = isset($data['county']) ? $data['county'] : $redata->county;
                $redata->district_name = isset($data['county']) ? Area::getNameByCode($data['county']) : $redata->district_name;
                $redata->address = isset($data['address']) ? $data['address'] : $redata->address;
                $redata->lng = isset($data['lng']) ? $data['lng'] : $redata->lng;
                $redata->lat = isset($data['lat']) ? $data['lat'] : $redata->lat;
                $redata->call = isset($data['phone']) ? $data['phone'] : $redata->phone;
                $redata->clinic_id = isset($data['clinic_id']) ? $data['clinic_id'] : $redata->clinic_id;
                $redata->hospital_id = isset($data['hospital_id']) ? $data['hospital_id'] : $redata->hospital_id;
                $redata->work_introduce = isset($data['work_introduce']) ? $data['work_introduce'] : $redata->work_introduce;
                if (isset($data['work_detail'])) {
                    $redata->work_detail = $tool::handelString($data['work_detail']);
                }
                $res = $redata->save();
            } else {
                //添加
                $this->user_id = isset($data['user_id'])?$data['user_id']:"";
                $this->hospital_id = isset($data['hospital_id'])?$data['hospital_id']:0;
                $this->logo =  isset($data['logo'])?$data['logo']:""; 
                $this->work_name =  isset($data['work_name'])?$data['work_name']:"";
                $this->status = 0;
                $this->province_id =  isset($data['province'])?$data['province']:"";
                $this->city_id =   isset($data['city'])?$data['city']:"";
                $this->district_id =  isset($data['county'])?$data['county']:"";
                $this->district_name =  isset($data['county'])?Area::getNameByCode($data['county']):"";
                $this->address =  isset($data['address'])?$data['address']:"";
                $this->lng =  isset($data['lng'])?$data['lng']:"";
                $this->lat =  isset($data['lat'])?$data['lat']:"";
                $this->call =  isset($data['phone'])?$data['phone']:"";
                $this->clinic_id =  isset($data['clinic_id'])?$data['clinic_id']:"";
                $this->work_introduce =  isset($data['work_introduce'])?$data['work_introduce']:"";
                if (isset($data['work_detail'])) {
                    $this->work_detail = $tool::handelString($data['work_detail']);
                }
                $res = $this->save();
            }
            if ($res) {
                $result = $this->addDoctorInfo($data);
                if ($result) {
                    DB::commit(); // 提交事务
                    return ['work_id' => $this->id ?: $redata->id];
                } else {
                    return '入驻失败';
                    DB::rollback(); // 回滚事务
                }
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }


    /***
     * @param $data
     * 存入草稿箱
     */
    public function getDoctorWorkBak($data)
    {
        $work_info = (new DoctorWorksBak())->with(['doctor', 'clinic', "province", "district", "city"])->where(['user_id' => $data['user_id']])->first();
        if (!$work_info) {
            return "暂无记录";
        }
        $tool =  new Tool();
        $work_info->host_logo = !empty($work_info->logo)?Url::getHttpHost()."upload/".$work_info->logo:null;
        if (!empty($work_info->work_detail)) {
            $detail = $work_info->work_detail;
            $work_info->host_work_detail = $tool->addHostToImage($detail);
        }
        $prof = (new Professional())->where([['id', '=', $work_info->doctor->professional_id]])->first();
        if ($prof) {
            $work_info->doctor->professional = $prof->toArray();
        } else {
            $work_info->doctor->professional = null;
        }
        $work_info->doctor->host_profile = !empty($work_info->doctor->profile)?Url::getHttpHost() . 'upload/' .  $work_info->doctor->profile:null;
        $work_info->doctor->host_front_card = !empty($work_info->doctor->front_card)?Url::getHttpHost() . 'upload/' .  $work_info->doctor->front_card:null;
        $work_info->doctor->host_behind_card = !empty($work_info->doctor->behind_card)?Url::getHttpHost() . 'upload/' .  $work_info->doctor->behind_card:null;
        if( $work_info->doctor->certificate){
            $uri = [];
            foreach( $work_info->doctor->certificate as $kk=> $vv){
                if(!empty($vv)){
                    $uri[] = Url::getHttpHost() . 'upload/' .$vv;
                }
            }
            $work_info->doctor->host_certificate  = $uri;
        }
        return $work_info->toArray();
    }
    
    
    

    /***
     * @param $work_id
     * 给工作室添加默认标签
     */
    public function addDefaultLabel($work_id)
    {
        $arr = ["关注患者", "门诊患者", "住院患者"];
        $data = [];
        foreach ($arr as $k => $v) {
            $data[$k]['label_name'] = $v;
            $data[$k]['work_id'] = $work_id;
            $data[$k]['is_default'] = 1;
            $data[$k]['num'] = $k + 1;
            $data[$k]['created_at'] = date("Y-m-d H:i:s", time());
            $data[$k]['updated_at'] = date("Y-m-d H:i:s", time());
        }
        return DB::table('c_address_book_labels')->insert($data);
    }

    /**
     * @param $work_id
     * @return bool
     * 添加默认指标名称
     */
    public function addDefaultTargets()
    {
        $arr = array(
            array("name" => '血压', 'unit' => 'mmHg'),
            array("name" => '血糖', 'unit' => 'mmol/l'),
            array("name" => '血脂', 'unit' => 'mg/dl'),
            array("name" => '尿酸', 'unit' => 'mmol/l'),
            array("name" => '转氨酸', 'unit' => 'U/L'),
            array("name" => '肌酐', 'unit' => 'umol/L'),
            array("name" => '入量', 'unit' => 'ml'),
            array("name" => '尿量', 'unit' => 'ml')
        );
        $data = [];
        foreach ($arr as $k => $v) {
            $data[$k]['name'] = $v['name'];
            $data[$k]['unit'] = $v['unit'];
            $data[$k]['is_default'] = 1;
            $data[$k]['created_at'] = date("Y-m-d H:i:s", time());
            $data[$k]['updated_at'] = date("Y-m-d H:i:s", time());
        }
        return DB::table('c_targets')->insert($data);
    }


    /***
     * @param $params
     * @return bool
     * 添加医师信息
     */
    public function addDoctorInfo($params)
    {
        $doctor = new DoctorBak();
        $tool = new Tool();
        $doctorInfo = (new DoctorBak())->where([['user_id','=',$params['user_id']]])->first();
        if(isset($params['hospital_id'])){
            $hospital =  (new Hospital())->where(['id'=>$params['hospital_id']])->first();
        }
        if ($doctorInfo) {
            $doctorInfo->real_name = isset($params['real_name'])?$params['real_name']:$doctorInfo->real_name;
            $doctorInfo->idcard = isset($params['idcard'])?$params['idcard']:$doctorInfo->idcard;
            $doctorInfo->clinic_id = isset($params['clinic_id'])?$params['clinic_id']:$doctorInfo->clinic_id;
//            $doctorInfo->hospital = isset($params['hospital'])?$params['hospital']:$doctorInfo->hospital;
            $doctorInfo->hospital = isset($hospital->hospital_name)? $hospital->hospital_name:$params['hospital'];
            $doctorInfo->hospital_id = isset($params['hospital_id'])? $params['hospital_id']:$doctorInfo->hospital_id;
            $doctorInfo->professional_id = isset($params['professional_id'])?$params['professional_id']:$doctorInfo->professional_id;
            $doctorInfo->front_card = isset($params['front_card'])?$params['front_card']:$doctorInfo->front_card;
            $doctorInfo->behind_card = isset($params['behind_card'])?$params['behind_card']:$doctorInfo->behind_card;
            $doctorInfo->profile = isset($params['profile'])?$params['profile']:$doctorInfo->profile;
            if (isset($params['certificate'])) {
                $doctorInfo->certificate = $tool::handelString($params['certificate']);
            }
            $doctorInfo->introduce = isset($params['introduce'])?$params['introduce']:$doctorInfo->introduce;
            $doctorInfo->skill = isset($params['skill'])?$params['skill']:$doctorInfo->skill;
            $doctorInfo->user_id = isset($params['user_id'])?$params['user_id']:$doctorInfo->user_id;
            $doctorInfo->phone = isset($params['phone'])?$params['phone']:$doctorInfo->phone;
            $doctorInfo->password = isset($params['password'])?$params['password']:$doctorInfo->password;
            $doctorInfo->account = isset($params['account'])?$params['account']:$doctorInfo->account;
            $doctorInfo->user_id = isset($params['user_id'])?$params['user_id']:$doctorInfo->user_id;
            $doctorInfo->age = isset($params['age']) ? $params['age'] : $doctorInfo->age;
            $doctorInfo->sex = isset($params['sex']) ? $params['sex'] : $doctorInfo->sex;
            $doctorInfo->save();
        } else {
            $doctor->real_name = isset($params['real_name'])?$params['real_name']:"";
            $doctor->idcard = isset($params['idcard'])?$params['idcard']:"";
            $doctor->clinic_id = isset($params['clinic_id'])?$params['clinic_id']:"";
//            $doctor->hospital = isset($params['hospital'])?$params['hospital']:"";
            $doctor->hospital = isset($hospital->hospital_name)? $hospital->hospital_name:$params['hospital'];
            $doctor->hospital_id = isset($params['hospital_id'])? $params['hospital_id']:0;
            $doctor->professional_id = isset($params['professional_id'])?$params['professional_id']:"";
            $doctor->front_card = isset($params['front_card'])?$params['front_card']:"";
            $doctor->behind_card = isset($params['behind_card'])?$params['behind_card']:"";
            $doctor->profile = isset($params['profile'])?$params['profile']:"";
            if (isset($params['certificate'])) {
                $doctor->certificate = $tool::handelString($params['certificate']);
            }
            $doctor->introduce = isset($params['introduce'])?$params['introduce']:"";
            $doctor->skill = isset($params['skill'])?$params['skill']:"";
            $doctor->user_id = isset($params['user_id'])?$params['user_id']:"";
            $doctor->phone = isset($params['phone'])?$params['phone']:"";
            $doctor->password = isset($params['password'])?$params['password']:"";
            $doctor->account = isset($params['account'])?$params['account']:"";
            $doctor->user_id = isset($params['user_id'])?$params['user_id']:"";
            $doctor->work_id = $this->id;
            $doctor->age = isset($params['age']) ? $params['age'] : "";
            $doctor->sex = isset($params['sex']) ? $params['sex'] : "";
            $doctor->save();
        }
        return true;
    }

    /***
     * @param $data
     * @return mixed
     * 获取工作室信息
     */
    public function getInfo($id)
    {
        return $this->where([['id', '=', $id]])->first();
    }

    /***
     * @param $data
     * @return mixed
     * 获取工作室信息
     */
    public function getDoctorInfoByWorkid($wid)
    {
        return (new Doctor())->where("work_id", '=', $wid)->first();
    }

    /***
     * @param $data
     * @return mixed
     * 获取工作室信息
     */
    public function getDoctorAccount($wid)
    {
        return (new Doctor())->where("account", '=', $wid)->first();
    }

    /**
     *
     * 通过城市获取code
     */
    public function getCityCode($data)
    {
        $where[] = ['id', ">", 35];
        $where[] = ['name', 'like', '%' . $data . '%'];
        $res = (new Area())->where($where)->orderBy("id", "desc")->first();
        if ($res) {
            return $res->code;
        }
        return 0;
    }

    public function getDoctorSearch($name)
    {
        return (new Doctor())->where([['real_name', 'like', '%' . $name . '%']])->distinct("work_id")->pluck("work_id")->toArray();
    }

    /****
     * @param $param
     * @return mixed
     * 工作室列表
     */


    public static function scope_distance($model, $from_latitude, $from_longitude, $distance)
    {
        $raw = DB::raw("round(st_distance_sphere(point($from_latitude, $from_longitude),point(latitude , longitude)), 2)/1000  AS distance");
        return $model->select('*')->addSelect($raw)->orderBy('distance', 'ASC')->having('distance', '<=', $distance);
    }


    public function getWorkList($param)
    {
        #经度
        $lng = $param["lng"];
        #纬度
        $lat = $param["lat"];
        #获取指定范围的数据单位km
        $distance_sea = 100;
        $where[] = ['status', '=', 1];
        $whereor = [];
        if (isset($param['clinic_id']) && !empty($param['clinic_id'])) {  //分类id
            $pid = (new ClinicCategory())->where([['pid', '=', $param['clinic_id']]])->pluck("id")->toArray();
            array_push($pid, $param['clinic_id']);
            $where[] = [function ($query) use ($pid) {
                $query->whereIn('clinic_id', $pid);
            }];
        }
        if (isset($param['is_home']) && !empty($param['is_home'])) {  //首页推荐
            $where[] = ['is_home', '=', $param['is_home']];
        }
        if (isset($param['city_name']) && !empty($param['city_name'])) {  //切换城市
//            $code = $this->getCityCode($param['city_name']);
//            $where[] = ['district_id', '=', $code];
            $where[] = ['district_name', 'like', '%' . $param['city_name'] . '%'];
        }
        if (isset($param['title']) && !empty($param['title'])) {  //名称
            $whereor = [];
            $where[] = ['work_name', 'like', '%' . $param['title'] . '%'];
            $case_id = $this->getDoctorSearch($param['title']);
            if (!empty($case_id)) {
                $whereor[] = [function ($query) use ($case_id) {
                    return $query->whereIn('id', $case_id);
                }];
            }
        }
        $model = DoctorWorks::where($where)->orWhere($whereor);
        $model->select(['*']);
        $distance = "ACOS(SIN(( $lat * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(( $lat* 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(( $lng* 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380";

//        $raw = DB::raw("round(st_distance_sphere(point($lat, $lng),point(latitude , longitude)), 2)/1000  AS distance");
//        $model->select('*')->addSelect($raw)->orderBy('distance', 'ASC')->having('distance', '<=', $distance_sea);

        $model->addSelect(DB::raw($distance . ' as distance'))->orderBy('distance', 'ASC');
        #控制范围
        $model->whereRaw($distance . "<" . $distance_sea);
        $data = $model->with(['clinic'])->paginate(15);
        $host = new Url();
        $tool = new Tool();
        $evals = new Evaluate();
        foreach ($data as $v) {
            #计算距离
            $v->distance = sprintf("%.2f", $v->distance) . "km";
            $v->order_nums = (new Order())->where([['work_id', '=', $v->id], ['order_status', '=', 1]])->count();
            $v->logo = $host::getHttpHost() . 'upload/' . $v->logo;
            $v->images = $host::getHttpHost() . 'upload/' . $v->images;
            if (!empty($v->work_detail)) {
                $detail = $v->work_detail;
                $v->work_detail = $tool->addHostToImage($detail);
            }
            //评价
            $v->evals = $evals->where([['work_id', '=', $v->id], ['score', '=', 5]])->orderBy("id", "desc")->first();
        }
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return [];
        }
    }

    public function detail($param)
    {
        $res = $this->where([['id', '=', $param['id']]])->first();
        if (empty($res)) {
            return [];
        }
        $res->province_name = (new Area())::getNameByCode($res->province_id);
        $res->city_name = (new Area())::getNameByCode($res->city_id);
        $res->county_name = (new Area())::getNameByCode($res->district_id);
        $res->statistics = $this->getGoodsOption($param['id']);
        $res->is_follow = 0;
        //是否关注 c_follows
        if (isset($param['user_id']) && !empty($param['user_id'])) {
            $is_follow = (new Follow())->where([['work_id', '=', $param['id']], ['user_id', '=', $param['user_id']], ['type', '=', 1]])->first();
            if ($is_follow) {
                $res->is_follow = 1;
            }
        }
        $host = new Url();
        $tool = new Tool();
        $res->logo = !empty($res->logo) ? $host::getHttpHost() . 'upload/' . $res->logo : "";
        $res->images = !empty($res->images) ? $host::getHttpHost() . 'upload/' . $res->images : $host::getHttpHost() . "image/default.jpg";
        if (!empty($res->work_detail)) {
            $detail = $res->work_detail;
            $res->work_detail = $tool->addHostToImage($detail);
        }
        $res->vocat_name = (new Professional())->where([['id', '=', $res->professional_id]])->value("name");
//        $res->logo = env("APP_URL").'/'. $res->logo;
        if (!empty($res)) {
            return $res->toArray();
        }
        return [];
    }

    /**
     * 计算好评率
     */
    public function getGoodsOption($work_id)
    {
        $data = [];
        $res = (new Evaluate())
            ->where([["work_id", '=', $work_id], ['types', '=', 3], ['score', '=', 5]])
            ->orderByDesc("id")
            ->first();
        $tool = new Tool();
        if (!empty($res)) {
            if ($res->images) {
                $res->images = $tool->addHostToImage($res->images);
            }
            $users = (new User())->where([['id', '=', $res->user_id]])->first();
            if ($users->image) {
                $users->image = Url::getHttpHost() . 'upload/' . $users->image;
            }
            $res->user = $users;
            $data['goods_option'] = $res;
        } else {
//            $res = (new Evaluate())
//                ->where([['types', '=', 3], ['score', '=', 5]])
//                ->orderByDesc("id")
//                ->first();
//            if ($res->images) {
//                $res->images = $tool->addHostToImage($res->images);
//            }
//            $users = (new User())->where([['id', '=', $res->user_id]])->first();
//            if ($users->image) {
//                $users->image = Url::getHttpHost() . 'upload/' . $users->image;
//            }
//            $res->user = $users;
            $data['goods_option'] = null;
        }
        //综合好评率=总评价条数÷好评条数
        $param['work_id'] = $work_id;
        $nums = (new Evaluate())->setNumber($param);
        $data['goods_rate'] = $nums['goods_rate'];
        $data['all'] = $nums['all'];
        return $data;
    }

    /***
     * 查询
     */
    public function getDoctorByWorkId($work_id)
    {
        $res = (new Doctor())->with(['clinic'])->where([['work_id', '=', $work_id]])->get();
        if (!empty($res)) {
            $result = $res->toArray();
            $host = new Url();
            $tool = new Tool();
            $professional = new Professional();
            foreach ($result as $k => $v) {
                $result[$k]['profile'] = $host::getHttpHost() . 'upload/' . $v['profile'];
                $result[$k]['front_card'] = $host::getHttpHost() . 'upload/' . $v['front_card'];
                $result[$k]['behind_card'] = $host::getHttpHost() . 'upload/' . $v['behind_card'];
                if (!empty($v['certificate'])) {
//                    $dd = json_decode($v['certificate'], true);
//                    if (!empty($dd)) {
                    $result[$k]['certificate'] = $tool->addHostToImage($v['certificate']);
//                    }
                }
                $result[$k]['professional'] = $professional->where([['id', '=', $v['professional_id']]])->first()->toArray();
            }
            return $result;
        }
        return [];
    }

    /**
     * @param $filter
     * @return mixed
     * 修改广告图
     * 修改工作室信息
     */
    public function updateImage($filter)
    {
        $res = $this->getInfo($filter['work_id']);
//        $res->images = $filter['images'];
        $res->images = isset($filter['images']) ? $filter['images'] : $res->images;
        $res->to_url = isset($filter['to_url']) ? $filter['to_url'] : $res->to_url;
        $res->address = isset($filter['address']) ? $filter['address'] : $res->address;
        $res->province_id = isset($filter['province_id']) ? $filter['province_id'] : $res->province_id;
        $res->city_id = isset($filter['city_id']) ? $filter['city_id'] : $res->city_id;
        $res->district_id = isset($filter['district_id']) ? $filter['district_id'] : $res->district_id;
        $res->lng = isset($filter['lng']) ? $filter['lng'] : $res->lng;
        $res->lat = isset($filter['lat']) ? $filter['lat'] : $res->lat;
        return $res->save();
    }


    public function updateWorkInfo($data)
    {
        $tool = new Tool();
        $redata = $this->getInfo($data['work_id']);
        $redata->user_id = isset($data['user_id']) ? $data['user_id'] : $redata->user_id;
        $redata->logo = isset($data['logo']) ? $data['logo'] : $redata->logo;
        $redata->work_name = isset($data['work_name']) ? $data['work_name'] : $redata->work_name;
        $redata->province_id = isset($data['province']) && !empty($data['province']) ? $data['province'] : $redata->province;
        $redata->city_id = isset($data['city']) && !empty($data['city']) ? $data['city'] : $redata->city;
        $redata->district_id = isset($data['county']) && !empty($data['county']) ? $data['county'] : $redata->district_id;
        $redata->district_name = isset($data['county']) && !empty($data['county']) ? Area::getNameByCode($data['county']) : $redata->district_name;
        $redata->real_name = isset($data['real_name']) ? $data['real_name'] : $redata->real_name;
        $redata->address = isset($data['address']) ? $data['address'] : $redata->address;
        $redata->lng = isset($data['lng']) ? $data['lng'] : $redata->lng;
        $redata->lat = isset($data['lat']) ? $data['lat'] : $redata->lat;
        $redata->call = isset($data['phone']) ? $data['phone'] : $redata->phone;
        $redata->clinic_id = isset($data['clinic_id']) ? $data['clinic_id'] : $redata->clinic_id;
        $redata->work_introduce = isset($data['work_introduce']) ? $data['work_introduce'] : $redata->work_introduce;
        if (isset($data['work_detail'])) {
            $redata->work_detail = $tool::handelString($data['work_detail']);
        }
        if (isset($data['api_token'])) {
            $doc = (new Doctor())->where([['api_token', '=', $data['api_token']]])->first();
            if ($doc->is_admin != 1) {
                return "你无权修改工作室信息!";
            }
            $redata->save();
            Tool::SetDoctorAndUpToken($data['api_token'], $doc->account);
            return true;
        } else {
            return "修改失败!";
        }
    }

}
